<template>
  <div class="qingjia">
    <p>请假申请</p>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="请假类型" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择请假类型">
          <el-option label="事假" value="shanghai"></el-option>
          <el-option label="病假" value="beijing"></el-option>
          <el-option label="年假" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请假时间" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">&emsp;&emsp; 至</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date2" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="请假缘由" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180" />
      <el-table-column prop="name" label="姓名" width="180" />
      <el-table-column prop="desc" label="请假缘由" />
    </el-table>
  </div>
</template>


<script>
import dayjs from 'dayjs'
import process from '../../api/process';
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      tableData: [
        {
          date: '',
          name: '',
          desc: '',
        },

      ],
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择请假类型', trigger: 'change' }
        ],
        date1: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        date2: [
          { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请填写请假缘由', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      // console.log('date1:', dayjs(this.ruleForm.date1).format('YYYY-MM-DD'), typeof this.ruleForm.date1)
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          alert('提交成功');
          this.tableData.push({
            date: dayjs(this.ruleForm.date1).format('YYYY-MM-DD'),
            name: this.ruleForm.name,
            desc: this.ruleForm.desc,
          })
          
         const liucheng =  await process(ruleForm)
         console.log(liucheng);
          
        } else {
          console.log('提交失败！');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}



</script>
<style>
.qingjia {

  width: 1200px;
  margin: 40px auto;
}

.qingjia p {
  text-align: center;
  font-size: 20px;
}

.el-form-item {
  margin-bottom: 60px;

}
</style>